import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PurchaseInfoPage extends JPanel {
    public PurchaseInfoPage(CardLayout cardLayout, JPanel cardPanel) {
        setLayout(new BorderLayout());
        setBackground(Color.WHITE);

        // 返回按钮
        JPanel headerPanel = createHeaderPanel(cardLayout, cardPanel);
        add(headerPanel, BorderLayout.NORTH);

        // 卖家信息
        JPanel sellerPanel = createSellerPanel();
        add(sellerPanel, BorderLayout.CENTER);

        // 价格和购买按钮
        JPanel pricePanel = createPricePanel(cardLayout, cardPanel);
        add(pricePanel, BorderLayout.SOUTH);
    }

    private JPanel createHeaderPanel(CardLayout cardLayout, JPanel cardPanel) {
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.setBackground(new Color(240, 240, 240));
        headerPanel.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));

        JButton backButton = new JButton("← 返回");
        backButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        backButton.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        backButton.setBackground(new Color(240, 240, 240));
        backButton.addActionListener(e -> cardLayout.show(cardPanel, "productDetail"));
        headerPanel.add(backButton, BorderLayout.WEST);

        return headerPanel;
    }

    private JPanel createSellerPanel() {
        JPanel sellerPanel = new JPanel(new BorderLayout());
        sellerPanel.setBackground(Color.WHITE);
        sellerPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 卖家信息
        JLabel sellerLabel = new JLabel("卖家ID: 张三的小店");
        sellerLabel.setFont(new Font("微软雅黑", Font.BOLD, 16));
        sellerPanel.add(sellerLabel, BorderLayout.NORTH);

        // 卖家评分
        JPanel ratingPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 10));
        ratingPanel.setBackground(Color.WHITE);
        
        ratingPanel.add(new JLabel("评分: "));
        for (int i = 0; i < 5; i++) {
            ratingPanel.add(new JLabel("★"));
        }
        ratingPanel.add(new JLabel("4.9 (128评价)"));
        
        sellerPanel.add(ratingPanel, BorderLayout.CENTER);

        // 卖家其他信息
        JPanel infoPanel = new JPanel(new GridLayout(3, 1, 5, 5));
        infoPanel.setBackground(Color.WHITE);
        infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
        
        infoPanel.add(createInfoLabel("发货地: 长沙"));
        infoPanel.add(createInfoLabel("响应速度: 很快"));
        infoPanel.add(createInfoLabel("好评率: 98%"));
        
        sellerPanel.add(infoPanel, BorderLayout.SOUTH);

        return sellerPanel;
    }

    private JLabel createInfoLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        return label;
    }

    private JPanel createPricePanel(CardLayout cardLayout, JPanel cardPanel) {
        JPanel pricePanel = new JPanel(new BorderLayout());
        pricePanel.setBackground(Color.WHITE);
        pricePanel.setBorder(BorderFactory.createEmptyBorder(15, 20, 15, 20));

        // 价格信息
        JLabel priceLabel = new JLabel("￥899.00  包邮  发货地：长沙");
        priceLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        pricePanel.add(priceLabel, BorderLayout.NORTH);

        // 购买按钮
        JButton buyButton = new JButton("立即购买");
        buyButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        buyButton.setBackground(new Color(255, 80, 80));
        buyButton.setForeground(Color.WHITE);
        buyButton.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
        buyButton.addActionListener(e -> cardLayout.show(cardPanel, "confirmPurchase"));
        pricePanel.add(buyButton, BorderLayout.SOUTH);

        return pricePanel;
    }
}